package org.visionlibrary.image.filters.bluring;

import java.awt.Point;
import java.util.Arrays;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.model.WindowFilter;


public class Median extends WindowFilter {
	protected int width;
	protected int height;

	public Median(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public Median(int size) {
		this(size, size);
	}

	public Median() {
		this(3);
	}

	protected int getNewPixelVal(TiledImage src, Point p, int channel) {
//		Vector<Integer> medianv = new Vector<Integer>();
		int[] medianv = new int[windowHeight * windowWidth];
		Point pIter = new Point();
		int i = 0;
		for (pIter.y = 0; pIter.y < windowHeight; pIter.y++)
			for (pIter.x = 0; pIter.x < windowWidth; pIter.x++) {
				int val = getWindowPixelVal(src, pIter, channel);
//				medianv.add(new Integer(val));
				medianv[i] = val;
				i++;
			}

//		Collections.sort(medianv);
		Arrays.sort(medianv);
//		return medianv.elementAt(medianv.size() / 2).intValue();
		int mindex = medianv.length / 2;
		return medianv[mindex];
	}

	protected void setWindowProperties() {
		windowWidth = width;
		windowHeight = height;
		dstPointInWindow = new Point(width / 2, height / 2);
	}
}
